#pragma once

#include <iostream>
#include <cassert>

namespace luojie {

	template<typename T>
	class vector {

	public:
		typedef T* iterator;

		vector(){}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		size_t size()
		{
			return _finish - _start;
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}
		
		void push_back(const T& val)
		{
			if (_finish == _endofstorage)
			{
				size_t oldsize = size();
				size_t newcapacity = oldsize == 0 ? 4 : 2 * oldsize;

				T* temp = new T[newcapacity];
				memcpy(temp, _start, oldsize * sizeof(T));
				delete[] _start;

				_start = temp;
				_finish = _start + oldsize;
				_endofstorage = _start + newcapacity;
			}

			*(_finish) = val;
			++_finish;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};

	void test_vector01()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		v.push_back(7);
		v.push_back(8);
		v.push_back(9);
		v.push_back(10);

		for (size_t i = 0; i < v.size(); i++) {
			std::cout << v[i] << ' ';
		}

		std::cout << std::endl;

		vector<int>::iterator  it = v.begin();
		while (it != v.end())
		{
			std::cout << (*it) << " ";
			++it;
		}
	}
}